﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Net;

namespace buzztest1
{
    class BuzzSharp
    {
        // Fields
        protected const string BuzzPublicUrlFormat = "http://buzz.googleapis.com/feeds/{0}/public/posted";

        // Methods
        public BuzzSharp() { }

        /// <summary>
        /// Return public user's Buzz feed 
        /// </summary>
        /// <param name="user">username</param>
        /// <returns>string as XML data, see http://code.google.com/apis/buzz/documentation/ for more information</returns>
        public string GetRawBuzz(string user)
        {
            return ExecuteGetCommand(string.Format(BuzzPublicUrlFormat, user), null, null);
        }


        /// <summary>
        /// Return array of users' Buzz feeds
        /// </summary>
        /// <param name="users">users list</param>
        /// <returns>array of string as XML data, see http://code.google.com/apis/buzz/documentation/ for more information</returns>
        public string[] GetRawBuzz(string[] users)
        {
            string[] response = new string[users.Length];
            for(int i = 0; i<users.Length; i++)
            {
                response[i] = GetRawBuzz(users[i]);
            }
            return response;
        }

        public void GetBuzz(string user)
        {
            try
            {
                var parser = new GenericFeedParser();
                List<GenericFeedItem> feed = parser.ReadFeedItems(string.Format(BuzzPublicUrlFormat, user));
            }
            catch { }
        }

        #region Service commands
        protected static string ExecuteGetCommand(string url, string userName, string password)
        {
            string str;
            using (WebClient client = new WebClient())
            {
                if (!(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)))
                {
                    client.Credentials = new NetworkCredential(userName, password);
                }
                try
                {
                    using (Stream stream = client.OpenRead(url))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            str = reader.ReadToEnd();
                        }
                    }
                }
                catch (WebException exception)
                {
                    if (exception.Response is HttpWebResponse)
                    {
                        if ((exception.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
                        {
                            return null;
                        }
                        if ((exception.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
                        {
                            return null;
                        }
                    }
                    throw exception;
                }
            }
            return str;
        }

        protected static string ExecutePostCommand(string url, string userName, string password, string data)
        {
            WebRequest request = WebRequest.Create(url);
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                request.Credentials = new NetworkCredential(userName, password);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
                /*
                if (!string.IsNullOrEmpty(TwitterClient))
                {
                    request.Headers.Add("X-Twitter-Client", TwitterClient);
                }
                if (!string.IsNullOrEmpty(TwitterClientVersion))
                {
                    request.Headers.Add("X-Twitter-Version", TwitterClientVersion);
                }
                if (!string.IsNullOrEmpty(TwitterClientUrl))
                {
                    request.Headers.Add("X-Twitter-URL", TwitterClientUrl);
                }
                if (!string.IsNullOrEmpty(Source))
                {
                    data = data + "&source=" + HttpUtility.UrlEncode(Source);
                }/**/
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                ServicePointManager.Expect100Continue = false;
                request.ContentLength = bytes.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                    using (WebResponse response = request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }
        #endregion
        
    }
}
